<template>
  <div class="change-password">
    <el-form
      ref="ruleForm"
      class="form"
      label-position="right"
      label-width="160px"
      :model="ruleForm"
      :rules="rules"
    >
      <div class="title-container">
        <h3 class="title">{{ $t("changePassword.title") }}</h3>
      </div>
      <el-form-item prop="pass" :label="$t('changePassword.newPassword')">
        <el-input
          auto-complete="new-password"
          v-model="ruleForm.pass"
          show-password
          type="password"
        />
      </el-form-item>
      <el-form-item
        prop="checkPass"
        :label="$t('changePassword.confirmPassword')"
      >
        <el-input v-model="ruleForm.checkPass" show-password type="password" />
      </el-form-item>
      <div class="sp_center confirm">
        <el-button
          class="btn"
          :loading="loading"
          type="primary"
          style="margin-bottom: 30px"
          @click="submitForm('ruleForm')"
          >{{ $t("changePassword.confirm") }}</el-button
        >
      </div>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("Please input your password"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("Please enter your password again"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("The two passwords do not match!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
      loading: false,
    };
  },
  mounted() {},
  methods: {
    getCode() {
      this.$message({
        message: this.$t("changePassword.getCodeTip"),
        type: "success",
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let arr = this.storage.get("shopers");
          if (arr.length == 1) {
            arr[0].password = this.ruleForm.pass;
          } else {
            let user = this.storage.get("user");
            arr = arr.map((item) => {
              if (user) {
                if (user.account == item.account) {
                  item.password = this.ruleForm.pass;
                }
              } else {
                if (item.account == "cardconfig") {
                  item.password = this.ruleForm.pass;
                }
              }
              return item;
            });
          }
          this.storage.set("shopers", arr);
          this.$message({
            message: "update successfully",
            type: "success",
          });
          await this.$store.dispatch("user/logout");
          this.$router.push({
            path: "/login",
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss">
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.change-password {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  padding-top: 140px;
  .form {
    position: relative;
    width: 560px;
    max-width: 100%;
    padding: 20px 35px 0;
    background: white;
    margin: 0 auto;
    overflow: hidden;
    .title-container {
      position: relative;

      .title {
        font-size: 26px;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }

      .set-language {
        color: #fff;
        position: absolute;
        top: 3px;
        font-size: 18px;
        right: 0px;
        cursor: pointer;
      }
    }
    .code {
      display: flex;
    }
  }
  .confirm {
    width: 100%;
    .btn {
      width: 100%;
    }
  }
}
</style>
